{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 利用 PCA 对半导体制造数据降维"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "说明:\n",
    "\n",
    "将 `secom.data` 放在当前目录下。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PCA 算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def pca(dataMat, topNfeat=9999999):\n",
    "    meanVals = dataMat.mean(0)\n",
    "    meanRemoved = dataMat - meanVals #remove mean\n",
    "    covMat = cov(meanRemoved, rowvar=0)\n",
    "    eigVals,eigVects = linalg.eig(mat(covMat))\n",
    "    eigValInd = argsort(eigVals)            #sort, sort goes smallest to largest\n",
    "    eigValInd = eigValInd[:-(topNfeat+1):-1]  #cut off unwanted dimensions\n",
    "    redEigVects = eigVects[:,eigValInd]       #reorganize eig vects largest to smallest\n",
    "    lowDDataMat = meanRemoved * redEigVects#transform data into new dimensions    \n",
    "    reconMat = (lowDDataMat * redEigVects.T) + meanVals\n",
    "    return lowDDataMat, reconMat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据导入函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def loadDataSet(fileName, delim='\\t'):\n",
    "    fr = open(fileName)\n",
    "    stringArr = [line.strip().split(delim) for line in fr.readlines()]\n",
    "    datArr = [list(map(float,line)) for line in stringArr]\n",
    "    return mat(datArr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入数据，将 NaN 替换成平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def replaceNanWithMean(): \n",
    "    datMat = loadDataSet('secom.data', ' ')\n",
    "    numFeat = shape(datMat)[1]\n",
    "    for i in range(numFeat):\n",
    "        meanVal = mean(datMat[nonzero(~isnan(datMat[:,i].A))[0],i]) #values that are not NaN (a number)\n",
    "        datMat[nonzero(isnan(datMat[:,i].A))[0],i] = meanVal  #set NaN values to mean\n",
    "    return datMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dataMat = replaceNanWithMean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据预处理，减去均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "meanVals = mean(dataMat, 0)\n",
    "meanRemoved = dataMat - meanVals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 协方差与特征值分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  5.34151979e+07   2.17466719e+07   8.24837662e+06   2.07388086e+06\n",
      "   1.31540439e+06   4.67693557e+05   2.90863555e+05   2.83668601e+05\n",
      "   2.37155830e+05   2.08513836e+05   1.96098849e+05   1.86856549e+05\n",
      "   1.52422354e+05   1.13215032e+05   1.08493848e+05   1.02849533e+05\n",
      "   1.00166164e+05   8.33473762e+04   8.15850591e+04   7.76560524e+04\n",
      "   6.66060410e+04   6.52620058e+04   5.96776503e+04   5.16269933e+04\n",
      "   5.03324580e+04   4.54661746e+04   4.41914029e+04   4.15532551e+04\n",
      "   3.55294040e+04   3.31436743e+04   2.67385181e+04   1.47123429e+04\n",
      "   1.44089194e+04   1.09321187e+04   1.04841308e+04   9.48876548e+03\n",
      "   8.34665462e+03   7.22765535e+03   5.34196392e+03   4.95614671e+03\n",
      "   4.23060022e+03   4.10673182e+03   3.41199406e+03   3.24193522e+03\n",
      "   2.74523635e+03   2.35027999e+03   2.16835314e+03   1.86414157e+03\n",
      "   1.76741826e+03   1.70492093e+03   1.66199683e+03   1.53948465e+03\n",
      "   1.33096008e+03   1.25591691e+03   1.15509389e+03   1.12410108e+03\n",
      "   1.03213798e+03   1.00972093e+03   9.50542179e+02   9.09791361e+02\n",
      "   8.32001551e+02   8.08898242e+02   7.37343627e+02   6.87596830e+02\n",
      "   5.64452104e+02   5.51812250e+02   5.37209115e+02   4.93029995e+02\n",
      "   4.13720573e+02   3.90222119e+02   3.37288784e+02   3.27558605e+02\n",
      "   3.08869553e+02   2.46285839e+02   2.28893093e+02   1.96447852e+02\n",
      "   1.75559820e+02   1.65795169e+02   1.56428052e+02   1.39671194e+02\n",
      "   1.28662864e+02   1.15624070e+02   1.10318239e+02   1.08663541e+02\n",
      "   1.00695416e+02   9.80687852e+01   8.34968275e+01   7.53025397e+01\n",
      "   6.89260158e+01   6.67786503e+01   6.09412873e+01   5.30974002e+01\n",
      "   4.71797825e+01   4.50701108e+01   4.41349593e+01   4.03313416e+01\n",
      "   3.95741636e+01   3.74000035e+01   3.44211326e+01   3.30031584e+01\n",
      "   3.03317756e+01   2.88994580e+01   2.76478754e+01   2.57708695e+01\n",
      "   2.44506430e+01   2.31640106e+01   2.26956957e+01   2.16925102e+01\n",
      "   2.10114869e+01   2.00984697e+01   1.86489543e+01   1.83733216e+01\n",
      "   1.72517802e+01   1.60481189e+01   1.54406997e+01   1.48356499e+01\n",
      "   1.44273357e+01   1.42318192e+01   1.35592064e+01   1.30696836e+01\n",
      "   1.28193512e+01   1.22093626e+01   1.15228376e+01   1.12141738e+01\n",
      "   1.02585936e+01   9.86906139e+00   9.58794460e+00   9.41686288e+00\n",
      "   9.20276340e+00   8.63791398e+00   8.20622561e+00   8.01020114e+00\n",
      "   7.53391290e+00   7.33168361e+00   7.09960245e+00   7.02149364e+00\n",
      "   6.76557324e+00   6.34504733e+00   6.01919292e+00   5.81680918e+00\n",
      "   5.44653788e+00   5.12338463e+00   4.79593185e+00   4.47851795e+00\n",
      "   4.50369987e+00   4.27479386e+00   3.89124198e+00   3.56466892e+00\n",
      "   3.32248982e+00   2.97665360e+00   2.61425544e+00   2.31802829e+00\n",
      "   2.17171124e+00   1.99239284e+00   1.96616566e+00   1.88149281e+00\n",
      "   1.79228288e+00   1.71378363e+00   1.68028783e+00   1.60686268e+00\n",
      "   1.47158244e+00   1.40656712e+00   1.37808906e+00   1.27967672e+00\n",
      "   1.22803716e+00   1.18531109e+00   9.38857180e-01   9.18222054e-01\n",
      "   8.26265393e-01   7.96585842e-01   7.74597255e-01   7.14002770e-01\n",
      "   6.79457797e-01   6.37928310e-01   6.24646758e-01   5.34605353e-01\n",
      "   4.60658687e-01   4.24265893e-01   4.08634622e-01   3.70321764e-01\n",
      "   3.67016386e-01   3.35858033e-01   3.29780397e-01   2.94348753e-01\n",
      "   2.84154176e-01   2.72703994e-01   2.63265991e-01   2.45227786e-01\n",
      "   2.25805135e-01   2.22331919e-01   2.13514673e-01   1.93961935e-01\n",
      "   1.91647269e-01   1.83668491e-01   1.82518017e-01   1.65310922e-01\n",
      "   1.57447909e-01   1.51263974e-01   1.39427297e-01   1.32638882e-01\n",
      "   1.28000027e-01   1.13559952e-01   1.12576237e-01   1.08809771e-01\n",
      "   1.07136355e-01   8.60839655e-02   8.50467792e-02   8.29254355e-02\n",
      "   7.03701660e-02   6.44475619e-02   6.09866327e-02   6.05709478e-02\n",
      "   5.93963958e-02   5.22163549e-02   4.92729703e-02   4.80022983e-02\n",
      "   4.51487439e-02   4.30180504e-02   4.13368324e-02   4.03281604e-02\n",
      "   3.91576587e-02   3.54198873e-02   3.31199510e-02   3.13547234e-02\n",
      "   3.07226509e-02   2.98354196e-02   2.81949091e-02   2.49158051e-02\n",
      "   2.36374781e-02   2.28360210e-02   2.19602047e-02   2.00166957e-02\n",
      "   1.86597535e-02   1.80415918e-02   1.72261012e-02   1.60703860e-02\n",
      "   1.49566735e-02   1.40165444e-02   1.31296856e-02   1.21358005e-02\n",
      "   1.07166503e-02   1.01045695e-02   9.76055340e-03   9.16740926e-03\n",
      "   8.78108857e-03   8.67465278e-03   8.30918514e-03   8.05104488e-03\n",
      "   7.56152126e-03   7.26347037e-03   7.31508852e-03   6.65728354e-03\n",
      "   6.50769617e-03   6.28009879e-03   6.19160730e-03   5.64130272e-03\n",
      "   5.30195373e-03   5.07453702e-03   4.47372286e-03   4.32543895e-03\n",
      "   4.22006582e-03   3.97065729e-03   3.75292740e-03   3.64861290e-03\n",
      "   3.38915810e-03   3.27965962e-03   3.06633825e-03   2.99206786e-03\n",
      "   2.83586784e-03   2.74987243e-03   2.31066313e-03   2.26782346e-03\n",
      "   1.82206662e-03   1.74955624e-03   1.69305161e-03   1.66624597e-03\n",
      "   1.55346749e-03   1.51278404e-03   1.47296800e-03   1.33617458e-03\n",
      "   1.30517592e-03   1.24056353e-03   1.19823961e-03   1.14381059e-03\n",
      "   1.13027458e-03   1.11081803e-03   1.08359152e-03   1.03517496e-03\n",
      "   1.00164593e-03   9.50024604e-04   8.94981182e-04   8.74363843e-04\n",
      "   7.98497544e-04   7.51612219e-04   6.63964301e-04   6.21097643e-04\n",
      "   6.18098604e-04   5.72611403e-04   5.57509231e-04   5.47002381e-04\n",
      "   5.27195077e-04   5.11487997e-04   4.87787872e-04   4.74249071e-04\n",
      "   4.52367688e-04   4.24431101e-04   4.19119024e-04   3.72489906e-04\n",
      "   3.34002143e-04   3.38125455e-04   2.97951371e-04   2.84845901e-04\n",
      "   2.79038288e-04   2.77054476e-04   2.67962796e-04   2.54815125e-04\n",
      "   2.29230595e-04   1.99245436e-04   1.90381389e-04   1.84497913e-04\n",
      "   1.77415682e-04   1.68160613e-04   1.63992030e-04   1.58025552e-04\n",
      "   1.54226003e-04   1.46890640e-04   1.46097434e-04   1.40079892e-04\n",
      "   1.35736724e-04   1.22704035e-04   1.16752515e-04   1.14080847e-04\n",
      "   1.04252870e-04   9.90265095e-05   9.66039063e-05   9.60766569e-05\n",
      "   9.16166337e-05   9.07003475e-05   8.60212634e-05   8.32654025e-05\n",
      "   7.70526075e-05   7.36470021e-05   7.24998306e-05   6.80209909e-05\n",
      "   6.68682701e-05   6.14500432e-05   5.99843180e-05   5.49918002e-05\n",
      "   5.24646951e-05   5.13403845e-05   5.02336253e-05   4.89288504e-05\n",
      "   4.51104474e-05   4.29823765e-05   4.18869715e-05   4.14341561e-05\n",
      "   3.94822846e-05   3.80307292e-05   3.57776535e-05   3.43901591e-05\n",
      "   2.98089203e-05   2.72388358e-05   2.42608885e-05   2.30962279e-05\n",
      "   2.27807559e-05   2.14440814e-05   1.96208174e-05   1.91217363e-05\n",
      "   1.88276186e-05   1.66549051e-05   1.39779892e-05   1.46846459e-05\n",
      "   1.43753346e-05   1.21760519e-05   1.20295835e-05   1.13426750e-05\n",
      "   1.09258905e-05   1.02782990e-05   1.01021807e-05   9.72678794e-06\n",
      "   9.64538293e-06   9.23630203e-06   8.93991857e-06   8.34247969e-06\n",
      "   7.36188587e-06   7.20354827e-06   6.69282813e-06   6.49477814e-06\n",
      "   5.09342484e-06   5.31392219e-06   5.67034892e-06   5.91044555e-06\n",
      "   6.00244889e-06   4.65422046e-06   4.45482134e-06   4.11265577e-06\n",
      "   3.77558985e-06   3.65202838e-06   3.48065952e-06   2.78847699e-06\n",
      "   2.66299626e-06   2.57492503e-06   2.39210232e-06   2.06298821e-06\n",
      "   2.00824521e-06   1.76373602e-06   1.58273269e-06   1.32211395e-06\n",
      "   1.49813697e-06   1.44003524e-06   1.42489429e-06   1.10002716e-06\n",
      "   9.01008863e-07   8.49881106e-07   7.62521870e-07   6.57641102e-07\n",
      "   5.85636640e-07   5.33937361e-07   4.16077215e-07   3.33765858e-07\n",
      "   2.95575264e-07   2.54744632e-07   2.20144574e-07   1.86314522e-07\n",
      "   1.77370966e-07   1.54794344e-07   1.47331686e-07   1.39738552e-07\n",
      "   1.04110968e-07   1.00786519e-07   9.38635089e-08   9.10853310e-08\n",
      "   8.71546321e-08   7.48338889e-08   6.06817434e-08   5.66479200e-08\n",
      "   5.24576912e-08   4.57020648e-08   2.89942623e-08   2.60449418e-08\n",
      "   2.10987990e-08   2.17618740e-08   1.75542294e-08   1.34637028e-08\n",
      "   1.27167434e-08   1.23258199e-08   1.04987513e-08   9.86367961e-09\n",
      "   8.49421567e-09   9.33428123e-09   7.42189373e-09   6.46870571e-09\n",
      "   6.84633763e-09   5.76455806e-09   5.01137947e-09   3.48686437e-09\n",
      "   2.91267161e-09   2.77880627e-09   1.73093431e-09   1.42391208e-09\n",
      "   1.16455105e-09   1.11815903e-09   9.24976262e-10   6.95072923e-10\n",
      "   1.80003529e-10   1.97062231e-10   2.61924413e-10   6.13291166e-10\n",
      "   5.27572950e-10   1.91829305e-15  -2.35986936e-15   1.25030565e-16\n",
      "  -7.04820952e-17  -5.30736004e-19   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00\n",
      "   0.00000000e+00   0.00000000e+00]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "590"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "covMat = cov(meanRemoved, rowvar=0)\n",
    "eigVals, eigVects = linalg.eig(mat(covMat))\n",
    "print(eigVals); len(eigVals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算主成分所占方差比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEg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CvOTufblmhr0Oz0nYR3QLfZEc\nzfEyyaPon05NW0XygwwwkeS/873AL4HzK1jbu0n+2/gCsCH1dTNwJ3Bnqs0KYBPJI/9PA/+ugvWd\nn3rf51M1pNdfZn0GrE6t3xeBrgr/fhMkw3paxrRQ1x/JDc0bwCmSe5MfJXkc54fAK6nvTam2XcBX\nMpb9SOqz2Av8pwrV1kuyrzr9GUyPHpsNrC30Wajg+vvH1OfrBZKBfV52jannZ/29V6K+1PR/SH/u\nMtqGsg5L9aUzVEVEImg8d8uIiMg5UriLiESQwl1EJIIU7iIiEaRwFxGJIIW7iEgEKdxFRCJI4S4i\nEkH/H+JA1Qgd7h+eAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2891bd1ecf8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "Var = eigVals\n",
    "Var_sum = sum(Var)\n",
    "Var_rate = Var/Var_sum\n",
    "plt.plot(Var_rate[:20],'s-') # plot 20st\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算主成分累计方差百分比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xt4XHd95/H3R3ffI8dyLr6HONkE\nQhJW6wbSQigkMSnFgbas06Wk0NbbbcNSoJdkywYeA1va3W27bPNAU2puT8GhsCR+wCW4TSgUCFgh\ndhI7JFGc2JLlxIrku6z7d/+YI3ssj0ZH1kgj6XxezzOPzvzO78x853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vpz7b8ymY+gXOmIfPgUpTZ8JJWku8HXgDyLi6LDVPyU3XHE1\n8H+B+yeztsT1EfEa4C3A70t6/bD1U2Ef1gBvA/6xwOqpsA/TmAr78U+BfuAfRugy2nthonwaeAVw\nDXCA3BDKcGXff8BtFD/KL9f+OyfTMfRbgWV595cCbSP1kVQFLODc/rQ8J5KqyQX+P0TE/xu+PiKO\nRsTxZHkrUC1p0WTVlzxvW/LzIPANcn9G50uznyfaW4CfRsRLw1dMhX0IvDQ05JX8PFigT1n3Y/LB\n8VuB/xTJAPRwKd4LEyIiXoqofPVaAAABpklEQVSIgYgYBP5uhOct9/6rAt4B3DdSn3Ltv3M1HUN/\nO7Ba0qrkSHA9sGVYny3A0CyJXwUeGukNX2rJ+N/fA09FxF+O0OfCoc8YJK0h9+/QMRn1Jc85R9K8\noWVyH/g9OazbFuDdySye64AjQ0MZk2jEI6xy78NE/vvsduCBAn0eBG6SVJ8MX9yUtE04SWuBPwHe\nFhFdI/RJ816YqPryPyN6+wjPm+b/+0R6M/CziGgttLKc+++clfuT5HO5kZtZ8gy5T/X/NGnbSO7N\nDVBHbkigGfgJcMkk1vbz5P78fBzYkdxuAX4X+N2kzx3ALnIzER4BXjfJ+++S5Ll3JnUM7cP8GgXc\nk+zjJ4DGSa5xNrkQX5DXVrZ9SO6XzwGgj9zR52+R+5zoX4Bnk58Lk76NwGfztn1v8l5sBt4zifU1\nkxsPH3ofDs1ouxjYWuy9MEn1fSl5bz1OLsgvGl5fcv+s/++TUV/S/vmh91xe30nff6W8+Ru5ZmYZ\nMh2Hd8zM7Bw59M3MMsShb2aWIQ59M7MMceibmWWIQ9/MLEMc+mZmGeLQNzPLkP8PVXuJc6pM1+0A\nAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2892006acc0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Var = eigVals\n",
    "Var_sum = sum(Var)\n",
    "Var_add = zeros_like(Var)\n",
    "for i in range(len(Var)):\n",
    "    Var_add[i] = sum(Var[:i+1])/Var_sum\n",
    "plt.plot(Var_add[:20],'s-') # plot 20st \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 PCA 降维，保留 6 个最大特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lowDMat, reconMat = pca(dataMat, 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1567, 6)\n"
     ]
    }
   ],
   "source": [
    "print(lowDMat.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1567, 590)\n"
     ]
    }
   ],
   "source": [
    "print(reconMat.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
